
const db = wx.cloud.database();
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tapSelect: 1,
    QAList: [],
    wuZan: "cloud://huangjiajie-1za1j.6875-huangjiajie-1za1j-1302920149/gongxu/dianzan.png",
    youZan: "cloud://huangjiajie-1za1j.6875-huangjiajie-1za1j-1302920149/releasesIcon/dianzan.png",
    zanFlag: false
  },
  tabSelect: function (e) {
    this.setData({
      tapSelect: e.currentTarget.dataset.index
    })
    if (this.data.tapSelect == 1) {
      db.collection("questionsAnswers")
        .orderBy('releaseTime', 'asc')
        .get()
        .then(res => {
          console.log(res);
          var listss = res.data;
          for (const i in listss) {
            var t = this.format(listss[i].releaseTime);
            listss[i].releaseTime = t;
          }
          this.setData({
            QAList: res.data

          })
        })
    } else {
      db.collection("questionsAnswers")
        .orderBy('fabulous', 'asc')
        .get()
        .then(res => {
          console.log(res);
          var listss = res.data;
          for (const i in listss) {
            var t = this.format(listss[i].releaseTime);
            listss[i].releaseTime = t;
          }
          this.setData({
            QAList: res.data
          })
        })
    }

  },


  //时间戳转换为日期

  add0(m) {
    return m < 10 ? '0' + m : m
  },

  format(shijianchuo) {
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y + '-' +this.add0(m) + '-' + this.add0(d);
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    db.collection("questionsAnswers")
      .get()
      .then(res => {
        console.log(res.data);
        var listss = res.data;
        for (const i in listss) {
          var t = this.format(listss[i].releaseTime);
          listss[i].releaseTime = t;
        }
        console.log("listss:", listss);

        this.setData({
          QAList: listss
        })
      })
  },
  dianzan(e) {
    let id = e.target.dataset.id;
    let idx = e.target.dataset.index;

    console.log(e);

    if (this.data.zanFlag) {

    } else {
      db.collection("questionsAnswers")
        .doc(id)
        .update({
          data: {
            fabulous: _.inc(1)
          }
        })
        .then(res => {
          console.log(res);

          this.setData({
            zanFlag: true,
          })

        })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})